/*
 * File Name��PayMethodAdapter.java
 * Copyright��Copyright 2008-2014 CiWong.Inc. All Rights Reserved.
 * Description�� PayMethodAdapter.java
 * Modify By��bin
 * Modify Date��2014��4��7��
 * Modify Type��Add
 */
package com.ciwong.mobilepay.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.content.res.ResourcesCompat;
import android.text.TextUtils.TruncateAt;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

import com.ciwong.mobilelib.R;
import com.ciwong.mobilepay.bean.PayMethod;
import com.ciwong.mobilepay.util.PayUtil;
import com.ciwong.mobilepay.view.PayMethodContainer;

/**
 * 支付方式Adapter
 * 
 * @author bin
 * @version ciwong v.1.0 2014年4月7日
 * @since ciwong v.1.0
 */
public class PayMethodAdapter extends BaseAdapter
{
    private Context mContext;

    private List<PayMethod> payMethods;

    /**
     * 当前选中的支付方式
     */
    private PayMethod payMethod = new PayMethod();

    /**
     * 当前选中的CheckBox
     */
    private ImageView payMethodBox;
    

    //用于选择习币或者其他购买方式的互斥
    private int selectPosition;

    /**
     * 
     * 构造函数
     * 
     * @param context
     *            上下文对象
     * @param payMethods
     *            支付方式集合
     */
    public PayMethodAdapter(Context context, List<PayMethod> payMethods)
    {
        this.mContext = context;
        this.payMethods = payMethods;
    }

    @Override
    public int getCount()
    {
        return payMethods == null ? 0 : payMethods.size();
    }

    @Override
    public Object getItem(int position)
    {
        return payMethods == null ? null : payMethods.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    /**
     * 
     * 设置选中状态״̬
     * 
     * @param payMethodBox
     *            选中的CheckBox
     * @param position
     *            选中的项
     */
    public void setChecked(ImageView payMethodBox, int position)
    {
        if (this.payMethodBox != null && this.payMethodBox == payMethodBox)
        {
            this.payMethodBox.setSelected(false);
            this.payMethodBox = null;
            this.payMethod = new PayMethod();
        }
        else
        {
            if (this.payMethodBox != null)
            {
                this.payMethodBox.setSelected(false);
            }
            payMethodBox.setSelected(true);
            this.payMethodBox = payMethodBox;
            this.payMethod = (PayMethod) getItem(position);
        }
        
        selectPosition=position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View v = null;
        ViewHolder holder = null;
        if (convertView == null)
        {
            v = new PayMethodItem(mContext,position);
            holder = new ViewHolder();
            holder.payMethodName = (TextView) v
                    .findViewById(PayMethodItem.payMethodName);
            holder.payMethodHint = (TextView) v
                    .findViewById(PayMethodItem.payMethodHint);
            holder.payMethodIcon = (ImageView) v
                    .findViewById(PayMethodItem.payMethodIcon);
            v.setTag(holder);
        }
        else
        {
            v = convertView;
            holder = (ViewHolder) v.getTag();
        }

        PayMethod pm = (PayMethod) getItem(position);
        holder.payMethodName.setText(pm.getName());
        holder.payMethodHint.setText(pm.getHint());
        holder.payMethodIcon.setImageDrawable(pm.getIcon());
        
        //默认选择第一个微信支付
        if(position==0)
        {
        	ImageView payMethodBox = (ImageView) v
                    .findViewById(PayMethodAdapter.PayMethodItem.payMethodBoxIv);
        	setChecked(payMethodBox, position);
        }
        
        return v;
    }

    /**
     * 获取当前选中支付方式
     * 
     * @return 当前选中支付方式
     */
    public PayMethod getPayMethod()
    {
        return payMethod;
    }

    public static class ViewHolder
    {
        public TextView payMethodName, payMethodHint;

        public ImageView payMethodIcon;
    }

    public class PayMethodItem extends LinearLayout
    {
        public static final int payMethodName = 0xf0f0f1;

        public static final int payMethodHint = 0xf0f0f2;

        public static final int payMethodIcon = 0xf0f0f3;

        public static final int payMethodBox = 0xf0f0f4;

        public static final int payMethodBoxIv = 0xf0f0f5;
        
        private int mPosition=0;

        public PayMethodItem(Context context,int position)
        {
            super(context);
            mPosition=position;
            init();
        }

        private void init()
        {
            setGravity(Gravity.CENTER_VERTICAL);
            DisplayMetrics dm = getContext().getResources().getDisplayMetrics();
            int height = (int) TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_DIP, 80, dm);
            // LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,
            // height);
            setLayoutParams(new AbsListView.LayoutParams(
                    LayoutParams.FILL_PARENT, height));

            setMinimumHeight((int) TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_DIP, 80, dm));
            int left = (int) TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_DIP, 10, dm);
            int right = left;
            setPadding(left, 0, right, 0);
            setDescendantFocusability(LinearLayout.FOCUS_BLOCK_DESCENDANTS);

            int width = (int) TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_DIP, 30, dm);
//            Rect rect = new Rect(0, 0, width, width);
//            Drawable enabled = PayUtil.getDrawableFromJar(
//                    "/images_mobilepay/check_circle_press.png",
//                    PayMethodAdapter.class, rect);
            Drawable enabled = ResourcesCompat.getDrawable(getResources(), R.mipmap.check_circle_press, null);


//            Drawable press = PayUtil.getDrawableFromJar(
//                    "/images_mobilepay/check_circle_press.png",
//                    PayMethodAdapter.class, rect);
            Drawable press =ResourcesCompat.getDrawable(getResources(), R.mipmap.check_circle_press, null);

//            Drawable normal = PayUtil.getDrawableFromJar(
//                    "/images_mobilepay/check_circle_normal.png",
//            PayMethodAdapter.class, rect);
            Drawable normal =ResourcesCompat.getDrawable(getResources(), R.mipmap.check_circle_normal, null);


            Drawable selector = PayUtil.getDrawableSelector(new int[][]{
                    {android.R.attr.state_checked},
                    {android.R.attr.state_selected},
                    {android.R.attr.state_pressed}, {}}, new Drawable[]{
                    enabled, press, press, normal});
            selector.setBounds(0, 0, 50, 50);

            final ImageView payMethodVietualIv = new ImageView(getContext());
            payMethodVietualIv.setId(PayMethodItem.payMethodBoxIv);
            LayoutParams ll = new LayoutParams(width, width);
            ll.setMargins(0, 0, right, 0);
            payMethodVietualIv.setLayoutParams(ll);
             //payMethodVietualIv.setBackgroundDrawable(selector);
            payMethodVietualIv.setImageDrawable(selector);
            payMethodVietualIv.setScaleType(ScaleType.FIT_CENTER);
            addView(payMethodVietualIv);

            CheckBox payMethodBox = new CheckBox(getContext());
            payMethodBox.setId(PayMethodItem.payMethodBox);
            payMethodBox.setLayoutParams(new LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            payMethodBox.setButtonDrawable(selector);
            payMethodBox.setPadding(0, 0, (int) TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_DIP, 10, dm), 0);
            payMethodBox.setClickable(false);
            payMethodBox
                    .setButtonDrawable(new ColorDrawable(Color.TRANSPARENT));
            payMethodBox
                    .setOnCheckedChangeListener(new OnCheckedChangeListener()
                    {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked)
                        {
                            if (isChecked)
                            {
                                payMethodVietualIv.setSelected(true);
                            }
                            else
                            {
                                payMethodVietualIv.setSelected(false);
                            }
                        }
                    });
            // addView(payMethodBox);
            
//            if(mPosition==0)
//            {
//            	payMethodBox.setSelected(true);
//            	payMethodVietualIv.setSelected(true);
//            	
//            	
//            }

            LinearLayout container = new LinearLayout(getContext());
            LayoutParams lp = new LayoutParams(0, LayoutParams.FILL_PARENT,
                    1.0f);
            container.setGravity(Gravity.CENTER_VERTICAL);
            container.setOrientation(LinearLayout.VERTICAL);
            addView(container);
            container.setLayoutParams(lp);

            TextView payMethodName = new TextView(getContext());
            payMethodName.setId(PayMethodItem.payMethodName);
            lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            payMethodName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
            container.addView(payMethodName);
            payMethodName.setTextColor(Color.BLACK);
            payMethodName.setLayoutParams(lp);

            TextView payMethodHint = new TextView(getContext());
            payMethodHint.setTextColor(0xffc7c7c7);
            payMethodHint.setId(PayMethodItem.payMethodHint);
            payMethodHint.setEllipsize(TruncateAt.END);
            container.addView(payMethodHint);
            payMethodHint.setLayoutParams(lp);

            ImageView payMethodIcon = new ImageView(getContext());
            payMethodIcon.setId(PayMethodItem.payMethodIcon);
            payMethodIcon.setScaleType(ScaleType.FIT_XY);
            int mHeight = (int) TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_DIP, 35, dm);
            int mWidth = (int) TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_DIP, 35, dm);
//            payMethodIcon.setMaxHeight(mHeight);
//            payMethodIcon.setMaxWidth(mWidth);
            LayoutParams layoutParams = new LayoutParams(mWidth, mHeight);
            payMethodIcon.setLayoutParams(layoutParams);
            addView(payMethodIcon);
//            payMethodIcon.setLayoutParams(lp);

        }
    }
    
    //用于选择习币或者其他购买方式的互斥
    public ImageView getPayMethodBox( )
    {
    	return payMethodBox;
    }
    

    //用于选择习币或者其他购买方式的互斥
    public int getSelectPosition()
    {
    	return selectPosition;
    }
    
}
